<template>
  <div class="login_container">
    <!--登录块-->
    <div class="login_box">
      <!--表单区域-->
      <el-form ref="loginFormRef" :rules="loginRules" :model="loginForm" class="login_form" label-width="0px">
        <!--用户名-->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-aixin"></el-input>
        </el-form-item>
        <!--密码-->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" prefix-icon="iconfont icon-aixin" type="password"></el-input>
        </el-form-item>
        <!--按钮-->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginFrom">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data(){
    return{
      //表单数据对象
      loginForm:{
        username:"123456",
        password:"123456"
      },
      //验证对象
      loginRules:{
        //校验用户名
        username:[
          { required: true, message: '请输入用户名称', trigger: 'blur' },//必填项验证
          { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }//验证长度
        ],
        //校验密码
        password:[
          { required: true, message: '请输入用户密码', trigger: 'blur' },//必填项验证
          { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }//验证长度
        ],
      },
    };
  },
  methods:{
    //重置表单内容
    resetLoginFrom(){
      this.$refs.loginFormRef.resetFields();
    },
    login(){
      //完成验证是否成功,处理登录
      this.$refs.loginFormRef.validate(async valid=>{
        if(!valid) return;
        const {data:res}= await this.$http.post("login",this.loginForm);
        if(res.flag == "ok"){
          this.$message.success("登录成功");
          //跳转到home页面
          this.$router.push({path: "/home"});
          //存储user对象
          window.sessionStorage.setItem("user",res.user);
        }else{
          this.$message.error("登陆失败");
        }
      });
    },
  },
}
</script>


<style lang="less" scoped>
  .login_container{
    background-color: white;
    height:100%;
  }
  .login_box{
    width:450px;
    height:300px;
    background-color: #fff;
    border-radius: 3px;
    position:absolute;
    left:50%;
    top:50%;
    transform:translate(-50%,-50%);
  }
.btns{
  display:flex;
  justify-content: flex-end;
}
.login_form{
  position: absolute;
  bottom:0%;
  width:100%;
  padding:0 10px;
  box-sizing: border-box;

}
</style>
